package com.example.phone.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;

/**
 * Created by Administrator on 2018\12\24 0024.
 */

public abstract class BaseActivity extends AppCompatActivity{
    /**
     * 封装toast对象，避免一直显示
     */
    private static Toast toast;
    /**private static Toast toast;
     * 获取TAG的activity名称
     */
    protected final String TAG = this.getClass().getSimpleName();

    private Context mContext;

    /**
     * 全局调试开关，fragment中同样使用这个
     */
    public static final boolean DEBUG = true;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mContext = this;

        log(initLayout() + "");
        //设置布局
        setContentView(initLayout());

        //初始化控件
        initView();

        //设置数据源
        initData();
        log("******Activity*****" + TAG);
    }

    public void log(String msg) {
        if(DEBUG)
            Log.d(TAG, msg);
    }

    //设置数据源
    public abstract void initData();

    //初始化视图
    public abstract void initView();

    //设置布局
    public abstract int initLayout();



    public void toastShort(String msg) {
        if (null == toast) {
            toast = Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT);
            toast.show();
        } else {
            toast.setText(msg);
            toast.show();
        }
    }

    public Context getContext() {
        return mContext;
    }

    /**
     * activity跳转
     * @param cls 要跳转的活动
     */
    protected void goNextActivity(Class<?> cls) {
        Intent intent = new Intent(this,cls);
        startActivity(intent);
    }

    /**
     * activity跳转
     * @param cls 要跳转的活动
     * @param bundle 传递到跳转活动的数据
     */
    protected void getNextActivity(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent(this, cls);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        ActivityCollector.removeActivity(this);
        super.onDestroy();
    }
}
